import Vue from 'vue'
import store from '../store'
import Router from 'vue-router'
//首页
import indexs from '@/components/index'
import box from '@/components/box'
//404
import author from '@/components/author'
import error from '@/components/error'
//爱心助力
import love from '@/views/love/love'
import countyInfo from '@/views/love/countyInfo'
import cadreInfo from '@/views/love/cadreInfo'
import lovelist from '@/views/love/lovelist'
import lovefpzq from '@/views/love/lovefpzq'
import lovefplist from '@/views/love/lovefplist'
//财富生活
import wealth from '@/views/wealth/wealth'
//品质生活
import quality from '@/views/quality/quality'
import qualitylist from '@/views/quality/qualitylist'
//送礼神器
import gifts from '@/views/gift/gifts'
import receiveGifts from '@/views/gift/receiveGifts'
// 个人中心
import me from '@/views/me/me'
import login from '@/views/me/login'
import fastLogin from '@/views/me/fastLogin'
import register from '@/views/me/register'
import forgetPwd from '@/views/me/forgetPwd'
import agreement from '@/views/me/agreement'
import aboutme from '@/views/me/aboutme'
import email from '@/views/me/email'
import feedback from '@/views/me/feedback'
import uploadFile from '@/views/me/uploadFile'
//我的订单
import order from '@/views/orders/order'
import orderlist from '@/views/orders/orderlist'
import goodslist from '@/views/orders/goodslist'
//商品详情
import commodity from '@/views/orders/commodity'
//购物车
import shoppingCart from '@/views/orders/shoppingCart'
//购买 确认订单
import purchase from '@/views/orders/purchase'
import paymoney from '@/views/orders/paymoney'
// 发票信息
import invoice from '@/views/orders/invoice' 
// 地址管理
import receiving from '@/views/address/receiving'
import addReceiving from '@/views/address/addReceiving'

// 砸金蛋
import winegg from '@/views/lottery/winegg'
// 签到
import signIn from '@/views/activity/signIn'
// 体检预约
import appointment from '@/views/health/appointment'


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      component: indexs,
      children: [
        {
          path: '/',
          name: 'box',
          component: box,
          meta: {
            title: '首页'
          }
        },
        {
          path: '/love',
          name: 'love',
          component: love,
          meta: {
            title: '爱心助力'
          }
        },
        {
          path: '/wealth',
          name: 'wealth',
          component: wealth,
          meta: {
            title: '财富生活'
          }
        },
        {
          path: '/quality',
          name: 'quality',
          component: quality,
          meta: {
            title: '品质生活'
          }
        },
        {
          path: '/me',
          name: 'me',
          component: me,
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/author',
      name: 'author',
      component: author,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/lovelist',
      name: 'lovelist',
      component: lovelist,
      meta: {
        title: '爱心助力'
      }
    },
    {
      path: '/lovefpzq',
      name: 'lovefpzq',
      component: lovefpzq,
      meta: {
        title: '扶贫专区'
      }
    },
    {
      path: '/lovefplist',
      name: 'lovefplist',
      component: lovefplist,
      meta: {
        title: '扶贫县'
      }
    },
    {
      path: '/countyInfo',
      name: 'countyInfo',
      component: countyInfo,
      meta: {
        title: '扶贫县介绍'
      }
    },
    {
      path: '/cadreInfo',
      name: 'cadreInfo',
      component: cadreInfo,
      meta: {
        title: '扶贫干部介绍'
      }
    },
    {
      path: '/qualitylist',
      name: 'qualitylist',
      component: qualitylist,
      meta: {
        title: '全部精选'
      }
    },
    {
      path: '/gifts',
      name: 'gifts',
      component: gifts,
      meta: {
        title: '送礼神器'
      }
    },
    {
      path: '/receiveGifts',
      name: 'receiveGifts',
      component: receiveGifts,
      meta: {
        title: '领取礼品'
      }
    },
    {
      path: '/commodity',
      name: 'commodity',
      component: commodity,
      meta: {
        title: '商品详情'
      }
    },
    {
      path: '/goodslist',
      name: 'goodslist',
      component: goodslist,
      meta: {
        title: '商品清单',
        requiresAuth: true
      }
    },
    {
      path: '/orderlist',
      name: 'orderlist',
      component: orderlist,
      meta: {
        title: '订单详情',
        requiresAuth: true
      }
    },
    {
      path: '/order',
      name: 'order',
      component: order,
      meta: {
        title: '我的订单',
        requiresAuth: true
      }
    },
    {
      path: '/pay/purchase',
      name: 'purchase',
      component: purchase,
      meta: {
        title: '确认订单',
        requiresAuth: true
      }
    },
    {
      path: '/pay/paymoney',
      name: 'paymoney',
      component: paymoney,
      meta: {
        title: '确认支付',
        requiresAuth: true
      }
    },
    {
      path: '/shoppingCart',
      name: 'shoppingCart',
      component: shoppingCart,
      meta: {
        title: '购物车',
        requiresAuth: true
      }
    },
    {
      path: '/invoice',
      name: 'invoice',
      component: invoice,
      meta: {
        title: '发票信息',
        requiresAuth: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/fastLogin',
      name: 'fastLogin',
      component: fastLogin,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: register,
      meta: {
        title: '注册'
      }
    },
    {
      path: '/forgetPwd',
      name: 'forgetPwd',
      component: forgetPwd,
      meta: {
        title: '找回密码'
      }
    },
    {
      path: '/agreement',
      name: 'agreement',
      component: agreement,
      meta: {
        title: '服务条款'
      }
    },
    {
      path: '/aboutme',
      name: 'aboutme',
      component: aboutme,
      meta: {
        title: '关于我们'
      }
    },
    {
      path: '/email',
      name: 'email',
      component: email,
      meta: {
        title: '我的邮箱',
        requiresAuth: true
      }
    },
    {
      path: '/feedback',
      name: 'feedback',
      component: feedback,
      meta: {
        title: '意见反馈'
      }
    },
    {
      path: '/receiving',
      name: 'receiving',
      component: receiving,
      meta: {
        title: '地址管理',
        requiresAuth: true
      }
    },
    {
      path: '/addReceiving',
      name: 'addReceiving',
      component: addReceiving,
      meta: {
        title: '地址管理',
        requiresAuth: true
      }
    },
    {
      path: '/uploadFile',
      name: 'uploadFile',
      component: uploadFile,
      meta: {
        title: '文件上传'
      }
    },
    {
      path: '/winegg',
      name: 'winegg',
      component: winegg,
      meta: {
        title: '天天惊喜'
      }
    },
    {
      path: '/signIn',
      name: 'signIn',
      component: signIn,
      meta: {
        title: '签到有礼'
      }
    },
    {
      path: '/appointment',
      name: 'appointment',
      component: appointment,
      meta: {
        title: '体检预约'
      }
    },
    {
      path: '*',
      name: 'error',
      component: error,
      meta: {
        title: '404'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
    window.document.title = to.meta.title;
    if (store.state.routerTo == '') {
        store.commit('routerTo', to.fullPath);
    }
    // 下面注释  往生产放记得打开注释  9行
    if (to.path == '/author' && store.state.openId) {
        // 用户使用后退返回到授权页，则默认回到首页
        next('/')
        return false
    }
    if (!store.state.openId && store.state.userid != '' && to.path != '/author') {
        // 第一次进入项目
        next('/author')
        return false
    }

    if (to.name == 'box') {
        store.commit('love', 0);
    } else if (to.name == 'love') {
        store.commit('love', 1);
    } else if (to.name == 'wealth') {
        store.commit('love', 2);
    } else if (to.name == 'quality') {
        store.commit('love', 3);
    } else if (to.name == 'me') {
        store.commit('love', 4);
    }

    // 可以在路由元信息指定哪些页面需要登录权限
    let islogin = false;// 假设没有登录（这里应从接口获取）
    let userToken = store.state.userid;
    if (userToken) {
        islogin = true;
    } else {
        islogin = false;
    }

    if (to.meta.requiresAuth && !islogin) { // 需要登录授权，这里还需要判断是否登录
        next('/fastLogin') // 跳转到登录
        return
    }

    // 页面跳转（包含刷新），都要清除分项信息
    // 各个页面分项之前都要重新设定一下当前页面需要分项的内容
    store.commit('scan', '');

    next()
})

export default router;